package com.ay.weixin.autoreply.db;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import lizar.db.ibatis.Dao;

import com.ay.weixin.autoreply.domain.MsgNews;

public class IbatisAutoReplyNewsDao extends Dao {
	
	public void insert(MsgNews msg) throws SQLException {
		
		this.sqlMapper.insert("msg_news.insert", msg);
	}
	
	public List<MsgNews> getNewsList(long wxproject_id, long auto_reply_id) throws SQLException {
		Map<String, Object> keys = new LinkedHashMap<String, Object>();
		keys.put("wxproject_id", wxproject_id);
		keys.put("auto_reply_id", auto_reply_id);
		return (List<MsgNews>) this.sqlMapper.queryForList("msg_news.getNewsList", keys);
		
	}
	
	public int size_by_id(long wxproject_id, long auto_reply_id) throws SQLException {
		Map<String, Object> keys = new LinkedHashMap<String, Object>();
		keys.put("wxproject_id", wxproject_id);
		keys.put("auto_reply_id", auto_reply_id);
		return (Integer) this.sqlMapper.queryForObject("msg_news.size_by_id", keys);
	}
	
	public List<MsgNews> getNewsListByKey(long wxproject_id, String key) throws SQLException {
		Map<String, Object> keys = new LinkedHashMap<String, Object>();
		keys.put("wxproject_id", wxproject_id);
		keys.put("key", key);
		return (List<MsgNews>) this.sqlMapper.queryForList("msg_news.getNewsListByKey", keys);
		
	}
	
	public int size(long wxproject_id, String key) throws SQLException {
		Map<String, Object> keys = new LinkedHashMap<String, Object>();
		keys.put("wxproject_id", wxproject_id);
		keys.put("key", key);
		return (Integer) this.sqlMapper.queryForObject("msg_news.size", keys);
	}
	
	public MsgNews getById(long id) throws SQLException {
		return (MsgNews) this.sqlMapper.queryForObject("msg_news.getById", id);
	}
	
	public int getMaxPos(long wxproject_id, String key) throws SQLException {
		Map<String, Object> keys = new LinkedHashMap<String, Object>();
		keys.put("wxproject_id", wxproject_id);
		keys.put("key", key);
		Integer max = (Integer) this.sqlMapper.queryForObject("msg_news.getMaxPos", keys);
		if (max == null) {
			return 0;
		}
		return max + 1;
	}
	
	public int getMaxPosByAutoReplyId(long wxproject_id, long auto_reply_id) throws SQLException {
		Map<String, Object> keys = new LinkedHashMap<String, Object>();
		keys.put("wxproject_id", wxproject_id);
		keys.put("auto_reply_id", auto_reply_id);
		Integer max = (Integer) this.sqlMapper.queryForObject("msg_news.getMaxPosByAutoReplyId", auto_reply_id);
		if (max == null) {
			return 0;
		}
		return max + 1;
	}
	
	public void delete_by_id(long id) throws SQLException {
		this.sqlMapper.delete("msg_news.delete_by_id", id);
	}
	
	public void update(MsgNews msg) throws SQLException {
		
		this.sqlMapper.insert("msg_news.update", msg);
	}
	
	public boolean exists(long id) throws SQLException {
		Object obj = this.sqlMapper.queryForObject("msg_news.exists", id);
		if (obj == null)
			return false;
		return true;
	}
}
